package com.example.student.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.student.generated.entity.Course;
import com.example.student.generated.entity.StudentCourse;
import com.example.student.generated.mapper.CourseMapper;
import com.example.student.generated.service.IStudentCourseService;
import com.example.student.service.MyCourseService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

@Service
public class MyCourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements MyCourseService {
    @Resource
    private IStudentCourseService myStudentCourseService;
    /**
     * 删除课程以及其选课记录
     * @param id
     * @return (success)删除成功 (failure)删除失败
     */
    @Override
    public boolean deleteOne(Long id) {
        //先删除课程号对应的选课记录
        myStudentCourseService.remove(Wrappers.<StudentCourse>lambdaQuery().eq(StudentCourse::getCourseId, id));
        //删除课程记录
        boolean flag = removeById(id);
        return flag;
    }

    /**
     * 批量删除课程以及其选课记录
     * @param courseList
     * @return (failure)返回删除失败的课程Id (success)空字符串
     */
    @Override
    public String deleteBatch(List<Course> courseList) {
        StringBuilder res=new StringBuilder();
        for (Course course : courseList) {
            //先删除课程对应的选课记录
            myStudentCourseService.remove(Wrappers.<StudentCourse>lambdaQuery().eq(StudentCourse::getCourseId, course.getId()));
            //删除课程记录
            boolean flag = removeById(course.getId());
            if(flag) res.append(course.getId()).append(" ");
        }
        return res.toString();
    }
}
